<?php
/**
 * 产品在线订单生成文件
 *
 * @author		Arthur(ArthurXF@gmail.com)
 * @copyright	(c) 2006 by bizeway.com
 * @version		$Id$
 * @package		ArthurXF
 * @subpackage	deal
 */

require_once('config/config.inc.php');
require_once("class/deal.class.php");


$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];

//如果没登陆，则让填写用户个人信息，此时把产品信息也取过来
if(empty($user_id)){
	check::AlertExit("请先填写个人信息",$arrGWeb['WEB_ROOT_pre']."/deal/filluserinfo.php");
}

$objWebInit = new deal();

//数据库连接参数
$objWebInit->setDBG($arrGPdoDB);
$objWebInit->db();


//订单编号
if (empty($user_id)) {
	$deal_no = date('YmdHis').'_x'.mt_rand(0,100000);
}else{
	$deal_no = date('YmdHis').'_'.$user_id;
}

//用户名
if (empty($user_name)) {
	$user_name = '直接购买';
}

$arrDeal = array();
foreach($_SESSION['cart'] as $k=>$v){
	
	$arrDeal['deal_no'] = $deal_no;
	$arrDeal['user_id'] = $user_id;
	$arrDeal['user_name'] = $user_name;
	$arrDeal['product'][$k]['product_id'] = $v['id'];
	$arrDeal['product'][$k]['product_title'] = $v['title'];
	$arrDeal['product'][$k]['product_model'] = $v['model'];
	$arrDeal['product'][$k]['product_brand'] = $v['brand'];
	$arrDeal['product'][$k]['product_price'] = $v['my_price'];
	$arrDeal['product'][$k]['product_count'] = $v['count'];
	$arrDeal['deal_price'] += $v['deal_price'];	
}

//产品种类
$arrDeal['product_kind'] = count($_SESSION['cart']);

//以下是取用户信息
//如果已经登陆了，则从session里取
if(!empty($_SESSION['user_id'])){
	$arrDeal['contactor'] = $_SESSION['real_name'];
	$arrDeal['email'] = $_SESSION['email'];
	$arrDeal['tel'] = $_SESSION['tel'];
	$arrDeal['mobile'] = $_SESSION['mobile'];
	$arrDeal['company'] = $_SESSION['company_cn'];
	$arrDeal['address'] = $_SESSION['address']; 
	$arrDeal['postcode'] = $_SESSION['postcode'];
}

//如果没有商品提示
if(empty($_SESSION['cart'])) check::AlertExit("暂无商品!",$_SERVER['HTTP_REFERER']);

$objWebInit->saveInfo($arrDeal,0);
unset($_SESSION['cart']);//确认订单后把session购买记录清空
echo "<script>window.close();</script>";
?>